package com.dormitory.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.dormitory.R;
import com.dormitory.bean.FileBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by JHF on 2017/10/16.
 */

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Activity mActivity;
    private List<List<FileBean>> mList;


    public ExpandableListAdapter(Activity activity) {
        mActivity = activity;

        mList = new ArrayList<>();
    }
    public void updateList(List<List<FileBean>> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public FileBean getFileBean(int group, int child) {
        return mList.get(group).get(child+1);
    }


    @Override
    public int getGroupCount() {
        return mList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mList.get(groupPosition).size()-1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mList.get(groupPosition).get(childPosition+1);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = null;
        if(convertView != null) {
            view = convertView;
        }
        else {
            view = View.inflate(mActivity, R.layout.filelist_parent, null);
        }
        if(groupPosition == 0)
            ((TextView)view.findViewById(R.id.text_dormitory)).setText("本寝室");
        else
            ((TextView)view.findViewById(R.id.text_dormitory)).setText(mList.get(groupPosition).get(0).getDormitory());
        ((TextView)view.findViewById(R.id.text_file_amount)).setText(mList.get(groupPosition).size()-1+"个文件");
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = null;
        if(convertView != null) {
            view = convertView;
        }
        else {
            view = View.inflate(mActivity, R.layout.filelist_child, null);
        }
        ((TextView)view.findViewById(R.id.text_dormitory)).setText(mList.get(groupPosition).get(childPosition+1).getFileName());
        ((TextView)view.findViewById(R.id.text_user)).setText("上传者  "+mList.get(groupPosition).get(childPosition+1).getUserName());
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}